/**
 * Copyright (C) 2013 DaiKit.com - daikit4gxt module (admin@daikit.com)
 *
 *         Project home : http://code.daikit.com/daikit4gxt
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.daikit.daikit4gxt.client.ui.forms;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.daikit.commons.shared.bean.AbstractDkBeanWithId;


/**
 * Class to wrap any other object
 * 
 * @author tcaselli
 * @version $Revision$ Last modifier: $Author$ Last commit: $Date$
 * @param <M>
 */
public class MyValueWrapper<M extends Serializable> extends AbstractDkBeanWithId
{
	private static final long serialVersionUID = -3814486613730131918L;
	private M value;

	/**
	 * Default constructor
	 */
	public MyValueWrapper()
	{
		// Nothing done by default
	}

	/**
	 * Constructor
	 * 
	 * @param value
	 *           the value
	 */
	public MyValueWrapper(final M value)
	{
		this.value = value;
	}

	/**
	 * @return the value
	 */
	public M getValue()
	{
		return value;
	}

	/**
	 * @param value
	 *           the value to set
	 */
	public void setValue(final M value)
	{
		this.value = value;
	}

	/**
	 * Convert a list of model to the corresponding {@link MyValueWrapper} list
	 * 
	 * @param list
	 * @return the corresponding {@link MyValueWrapper} list
	 */
	public static <M extends Serializable> List<MyValueWrapper<M>> convert(final List<M> list)
	{
		final List<MyValueWrapper<M>> ret = new ArrayList<MyValueWrapper<M>>();
		for (final M value : list)
		{
			ret.add(new MyValueWrapper<M>(value));
		}
		return ret;
	}

	/**
	 * Convert a list of {@link MyValueWrapper} to the corresponding list of values
	 * 
	 * @param list
	 * @return the corresponding value list
	 */
	public static <M extends Serializable> List<M> convertBack(final List<MyValueWrapper<M>> list)
	{
		final List<M> ret = new ArrayList<M>();
		for (final MyValueWrapper<M> valueWrapper : list)
		{
			ret.add(valueWrapper.getValue());
		}
		return ret;
	}
}
